package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.domain.Tag;
import cn.broadsense.biz.domain.TagType;
import cn.broadsense.biz.dto.TagTypeTreeDto;
import cn.broadsense.biz.query.TagTypeQuery;
import cn.broadsense.biz.service.TagService;
import cn.broadsense.biz.service.TagTypeService;
import cn.broadsense.biz.vo.TypeVo;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author lijian
 * @description
 * @date 2024年09月09日 下午3:49
 */
@RestController
@Api(tags = "标签类型管理")
@RequestMapping("/tag/type")
@RequiredArgsConstructor
public class TagTypeController extends BaseController {

    private final TagTypeService tagTypeService;

    private final TagService tagService;

    @Log(title = "标签类型管理-新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增标签类型")
    @PostMapping("/add")
    public R addTagType(@Validated @RequestBody TypeVo vo) {
        tagTypeService.insertType(vo);
        return R.ok();
    }

    @Log(title = "标签类型管理-修改", businessType = BusinessType.UPDATE)
    @ApiOperation("修改标签类型")
    @PutMapping("/update/{typeId}")
    public R updateTagType(@Validated @RequestBody TypeVo vo,@PathVariable Integer typeId) {
        TagType one = tagTypeService.getById(typeId);
        if (one == null) {
            return R.fail("标签类型不存在");
        }
        tagTypeService.lambdaUpdate()
                .set(TagType::getTypeName,vo.getName())
                .set(TagType::getRemark,vo.getRemark())
                .eq(TagType::getTypeId,typeId)
                .update();
        return R.ok();
    }

    @Log(title = "标签类型管理-删除",businessType = BusinessType.DELETE)
    @ApiOperation("删除标签类型")
    @DeleteMapping("/{ids}")
    public R deleteTagType(@PathVariable Integer[] ids) {
        for (Integer id : ids) {
            Integer one = tagTypeService.getChild(id);
            if (one != null) {
                return R.fail("标签类型下有子类型，无法删除");
            }
        }
        return R.ok(tagTypeService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("标签类型详情")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id) {
        TagType one = tagTypeService.getById(id);
        if (one == null) {
            return R.fail("标签不存在");
        }
        one.setTypeNums(tagTypeService.getTypeNums(id));
        List<Tag> list = tagService.lambdaQuery().eq(Tag::getTypeId, id).select(Tag::getTypeId,Tag::getTagName, Tag::getCreateTime).list();
        one.setTags(list);
        return R.ok(one);
    }

    @ApiOperation("标签类型列表")
    @GetMapping("/list")
    public R<List<TagTypeTreeDto>> list(TagTypeQuery query){
        return R.ok(tagTypeService.getTypeTreeList(query));
    }


    @ApiOperation("标签类型下拉框")
    @GetMapping("/option")
    public R<List<TagTypeTreeDto>> option(){
        return R.ok(tagTypeService.getOptionTree());
    }

}
